import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { router } from '@/router'

import { ACCESS_TOKEN } from "@/store/mutation-types"
import baseConfig from "@/baseConfig";
// 创建 axios 实例
let baseURL = process.env.NODE_ENV == 'development' ? baseConfig.api_url : (process.env.NODE_ENV == 'production' ? baseConfig.api_url : '');
const service = axios.create({
  baseURL: baseURL,
  timeout: 16000 // 请求超时时间
})

const err = (error) => {
  if (error.response) {
    let data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    switch (error.response.status) {
      case 403:
        alert("系统提示")
        break
      default:
        alert("系统提示2")
        break
    }
  } else {
    alert("网络错误");
  }
  return Promise.reject(error)
};

// request interceptor(请求拦截)
service.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN);
  if (token) {
    config.headers['token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

// response interceptor（响应拦截）
service.interceptors.response.use((response) => {
  
  return response.data
}, err)



export {
  service as axios
}